package cn.javaxc.mvcwork.modules.admin;

import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import cn.javaxc.mvcwork.util.helper.*;
import java.sql.*;
import java.util.*;
import cn.javaxc.model.base.*;


public class UserPermissionList extends LinkedHashSet {
  private int roleId = -1;


  
  public UserPermissionList() {
  }


  
  public UserPermissionList(int roleId)  {
    
    buildList(roleId);
  }


  
  public void buildList(int roleId) {
	this.roleId = roleId;
    int items = -1;

//    StringBuffer sqlSelect = new StringBuffer();
//
//    //Need to build a base SQL statement for returning records
//    sqlSelect.append(
//        "SELECT p.*, role_add, role_view, role_edit, role_delete, " +
//        "role_offline_view, role_offline_add, role_offline_edit, role_offline_delete " +
//        "FROM permission p, ts_role_permission r " +
//        "WHERE  p.sid = r.permission_id ");
//    if (roleId > -1) {
//      sqlSelect.append("AND r.role_id = :role_id ");
//    }
//    sqlSelect.append("AND p.enabled = :p_enabled ");
//    sqlSelect.append("ORDER BY role_id,  p.level ");
//    String sql=sqlSelect.toString();
//	SQLMap pst=new SQLMap();
//    if (roleId > -1) {
//      pst.setInt("role_id", roleId);
//    }
//    pst.setBoolean("p_enabled", true);
//    pst.setBoolean("c_enabled", true);
//    rs = pst.executeQuery();
	IBaseDAO dao=FrameHelper.getBaseDAO();
	SQLList<SQLMap> elist = dao.getuserpermission(roleId);
//	elist.buildList(db,sql,"",pst.getMap());
//    while (rs.next()) {
	for(SQLMap rs:elist){
      Permission thisPermission = new Permission(rs);
//      this.addElement(thisPermission);
	  if (rs.getInt("role_add")>0) {
          add(thisPermission.getName() + ":add");
        }
	  if (rs.getInt("role_view")>0) {
          add(thisPermission.getName() + ":view");
        }
	  if (rs.getInt("role_edit")>0) {
          add(thisPermission.getName() + ":edit");
        }
	  if (rs.getInt("role_delete")>0) {
          add(thisPermission.getName() + ":delete");
        }
    }
	System.err.println(this.getClass().getName()+" "+this.size());
  }


}
